package module

import (
	"dub/app/gate/tcp/cmdefine"
	"dub/common"
	"dub/define/grpcmodel"
	"dub/utils"
	"github.com/golang/protobuf/proto"
)

type AdvertModule struct {
	log    *utils.Logger
	useRpc *utils.RpcProxy
}

func (a *AdvertModule) OnMessage(id uint16, data []byte, ses common.ISession) bool {
	result := true
	switch id {
	case cmdefine.CmdSub_Advert_Find:
		result = a.findAdverts(data, ses)
	}
	return result
}

//查询广告分享信息
func (a *AdvertModule) findAdverts(data []byte, ses common.ISession) bool {
	args := &grpcmodel.ModelSecRpcAdvertReqFind{}
	err := proto.Unmarshal(data, args)
	if err != nil {
		a.log.Errorf("advert_module.go findAdverts method proto.Unmarshal err. %v\n", err)
	}

	sec_advert := grpcmodel.NewISecRpcAdvertClient(a.useRpc.Conn())
	reply, err := sec_advert.Find(a.useRpc.Context(), args)
	if err != nil {
		a.log.Errorf("advert_module.go findAdverts method sec_advert.Find err. %v\n", err)
	}

	reData, err := proto.Marshal(reply)
	if err != nil {
		a.log.Errorf("advert_module.go findAdverts mothed proto.Marshal err. %v\n", err)
		return true
	}

	ses.Send(cmdefine.CmdMain_Advert, cmdefine.CmdSub_Advert_Find, reData)
	return true
}

func NewAdvertModule(rpc *utils.RpcProxy) common.IMoudle {
	return &AdvertModule{
		log:    utils.NewLogger(),
		useRpc: rpc,
	}
}
